//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    internal class SheetInfo : IOfficeXmlSerializable, IComparable
    {
        public SheetInfo()
        {
        }
        public SheetInfo(string name)
        {
            Name = name;
        }
        #region IComparable Members

        public int CompareTo(object obj)
        {
            return sheetId.CompareTo(((SheetInfo)obj).sheetId);
        }

        #endregion

        #region IOfficeXmlSerializable Members


        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("sheet")) 
            {
                name = c.GetString("name");
                sheetId = c.GetInteger("sheetId");
                State = c.GetOptionalEnum("state", SheetState.Visible);
                string id = c.GetOptionalString(OfficeConstants.RelationshipNamespace + "id");
                if (string.IsNullOrEmpty(id))
                    throw new IgnoreElementException();
                RelationshipId = id;
            }

        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("sheet"))
            {
                c.SetString("name", name);
                c.SetInteger("sheetId", sheetId);
                c.SetOptionalEnum("state", State, SheetState.Visible);
                c.SetString(OfficeConstants.RelationshipNamespace + "id", RelationshipId);
            }
        }
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int sheetId;

        public int SheetId
        {
            get { return sheetId; }
            set { sheetId = value; }
        }
        private SheetState _state;

        public SheetState State
        {
            get { return _state; }
            set { _state = value; }
        }
	
        private string _relationshipId;

        public string RelationshipId
        {
            get
            {
                Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
                return _relationshipId;
            }
            set
            {
                Contract.Requires(!string.IsNullOrEmpty(value));
                

                _relationshipId = value;
            }
        }




        #endregion

        internal void CopyTo(Sheet ws)
        {
            switch (State)
            {
                case SheetState.Visible:
                    ws._isVisible = true; break;
                case SheetState.Hidden:
                case SheetState.VeryHidden:
                    ws._isVisible = false; break;
                default: throw new NotSupportedException();
            }
        }

        internal void CopyFrom(Sheet workSheet)
        {
            if (workSheet.IsVisible)
                State = SheetState.Visible;
            else
                State = SheetState.Hidden;
        }
    }
}
